﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Facebook;
using FBApp.Classes;
using System.Collections.ObjectModel;
using System.Windows.Threading;

namespace FBApp
{
	/// <summary>
	/// Interaction logic for WallFeed.xaml
	/// </summary>
	public partial class WallFeed : UserControl
	{
        private LoadingIndicator indicator;
        private FacebookClient fbc = null;

        //Feeds
        private ObservableCollection<WallFeedItem> wallFeed { get; set; }
        private static Dictionary<string, JSONObject> wallFeedDictionary = new Dictionary<string, JSONObject>();

		public WallFeed()
		{
			this.InitializeComponent();
            wallFeed = new ObservableCollection<WallFeedItem>();
            icItems.ItemsSource = wallFeed;  
		}


        public void setIndicator(LoadingIndicator indicator)
        {
            this.indicator = indicator;
        }

        public void setFacebookClient(FacebookClient fbc)
        {
            this.fbc = fbc;
        }

        public List<JSONObject> updateWallFeed()
        {            
            JSONObject response = fbc.getFacebookAPI().Get("/me/feed");
            Dictionary<string, JSONObject> dictionary = response.Dictionary;

            JSONObject[] wallFeedItems = dictionary["data"].Array;

            List<JSONObject> newItems = new List<JSONObject>();

            for (int k = 0; k < wallFeedItems.Length; k++)
            {
                if (!wallFeedDictionary.ContainsKey(wallFeedItems[k].Dictionary["id"].ToDisplayableString()))
                {
                    wallFeedDictionary[wallFeedItems[k].Dictionary["id"].ToDisplayableString()] = wallFeedItems[k];
                    newItems.Add(wallFeedItems[k]);
                }
            }            
           // fbc.getNotifier().displayNotification(newItems.Count + " wall feed items added");
            return newItems;
        }

        public void addWallFeedItem(JSONObject item)
        {
            Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate()
            {
                if(item.Dictionary["type"].ToDisplayableString().Equals("status"))
                {
                    WallFeedItem wallfeedItem = null;
                    if (item.Dictionary.ContainsKey("to"))
                    {
                        wallfeedItem = new WallFeedItem(item.Dictionary["id"].ToDisplayableString(), item.Dictionary["from"].Dictionary["name"].ToDisplayableString(), item.Dictionary["from"].Dictionary["id"].ToDisplayableString(), item.Dictionary["to"].Dictionary["data"].Array[0].Dictionary["name"].ToDisplayableString(), item.Dictionary["to"].Dictionary["data"].Array[0].Dictionary["id"].ToDisplayableString(), item.Dictionary["message"].ToDisplayableString(), item.Dictionary["type"].ToDisplayableString(), item.Dictionary["created_time"].ToDisplayableString(), item.Dictionary["updated_time"].ToDisplayableString());                        
                    }
                    else
                    {
                        wallfeedItem = new WallFeedItem(item.Dictionary["id"].ToDisplayableString(), item.Dictionary["from"].Dictionary["name"].ToDisplayableString(), item.Dictionary["from"].Dictionary["id"].ToDisplayableString(), "", "", item.Dictionary["message"].ToDisplayableString(), item.Dictionary["type"].ToDisplayableString(), item.Dictionary["created_time"].ToDisplayableString(), item.Dictionary["updated_time"].ToDisplayableString());
                    }

                    if (item.Dictionary.ContainsKey("comments"))
                    {
                        JSONObject[] list = item.Dictionary["comments"].Dictionary["data"].Array;
                        for (int k = 0; k < list.Length; k++)
                        {                           
                            CommentItem ci = new CommentItem(list[k].Dictionary["id"].ToDisplayableString(), list[k].Dictionary["from"].Dictionary["name"].ToDisplayableString(), list[k].Dictionary["from"].Dictionary["id"].ToDisplayableString(), list[k].Dictionary["message"].ToDisplayableString(), list[k].Dictionary["created_time"].ToDisplayableString());
                            wallfeedItem.addComment(ci);
                        }                        
                    }

                    lock (wallFeed)
                    {
                        wallFeed.Add(wallfeedItem);
                    }                                        
                }                
            }));
        }

        public void refreshUI()
        {
            Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate()
            {
                icItems.UpdateLayout();
            }
             ));
        }
	}
}